
$(function(){
     var serviceName=SYS_GLOBAL_CONFIG.getQueryString("serviceName");
        $(".search-center .search-name").html(serviceName);
        var serviceId=parseInt(SYS_GLOBAL_CONFIG.getQueryString("serviceId"));
    
        //获取当前城市名
        if(SYS_GLOBAL_CONFIG.getQueryString("currentCity")){
            currentCity=SYS_GLOBAL_CONFIG.getQueryString("currentCity");
        }else{
            if(window.localStorage){
                if(localStorage.currentCity){
                    currentCity=localStorage.currentCity;
                }else{
                    localStorage.currentCity=currentCity="深圳";
                }
            }else{
                currentCity="深圳";
            }
        }
        var areaId;
        var userId=localStorage.userId;
        /**
         *点击显示下拉选择
         **/    
        function b(a){
            var $_this=a;
            $_this.css("color","#36c7b7");
            var attrS=$_this.find("i").attr("class");
            var attrs=attrS.split(" ");
            if(attrs[1]==="arrow-bottom36c"||attrs[1]==="arrow-bottom"){
                $_this.find("i").attr("class","triangle arrow-top36c");  
            }else{   
                $_this.find("i").attr("class","triangle arrow-bottom36c");
            }
            $_this.siblings("li").css("color","#666").find("i").addClass("arrow-bottom").removeClass("arrow-bottom36c").removeClass("arrow-top36c").removeClass("arrow-top");
        }
        //点击显示分类
        $(".near-service").off();
        $(".near-service").on("click",function(){
            if($(".choose").css("display")=="none"){
                $('html').css({"height":"100%","overflow":"hidden"});
                $('body').css({"height":"100%","overflow":"hidden"});
            }else{
                  $('html').css({"overflow":"auto"});
                $('body').css({"overflow":"auto"});
            }
            $(".choose-name1").css("color","#36c7b7");
            $(".choose-name2").css("color","#666");
            $(".choose").slideToggle(100).siblings(".serviceSelect").hide();
            
            b($(this));
            var attrS=$(this).find("i").attr("class")
            console.info(attrS);
            var attrs=attrS.split(" ");
            console.info(attrs[1]);
            if(attrs[1]==="arrow-top36c"||attrs[1]==="arrow-top"){
                $(".shadow").css("display","block")
            }else{
                $(".shadow").css("display","none");
            }
            requestData3=JSON.stringify(
                {
                    "code":"getDictAreaList",
                    "version":"1.0",
                    "data":{
                        "level":"2",
                        "fullName":currentCity
                    }
                });
            getData(url,requestData3,handdle_func3,async,type);  
        });
        //点击显示地区s
        $(".new-service").on("click",function(){
            if($(".sort").css("display")=="none"){
                $('html').css({"height":"100%","overflow":"hidden"});
                $('body').css({"height":"100%","overflow":"hidden"});
            }else{
                  $('html').css({"overflow":"auto"});
                $('body').css({"overflow":"auto"});
            }
            $(".sort").slideToggle(100).siblings(".serviceSelect").hide();
            b($(this));
            var attrS=$(this).find("i").attr("class")
            console.info(attrS);
            var attrs=attrS.split(" ");
            console.info(attrs[1]);
            if(attrs[1]==="arrow-top36c"||attrs[1]==="arrow-top"){
                $(".shadow").css("display","block")
            }else{
                $(".shadow").css("display","none");
            }
            $(".choose-name1").css("color","#666");
            $(".choose-name2").css("color","#36c7b7");
        });
        $(".sort-ul .sort-li").on("click",function(){
             $('html').css({"overflow":"auto"});
                $('body').css({"overflow":"auto"});
            $(this).addClass("bgf036c").siblings(".sort-li").removeClass("bgf036c");
             $(".sort").hide();
             $(".shadow").css("display","none");
             $(".choose-name2").html($(this).html());
             $(".choose-name2").siblings("i").addClass("arrow-bottom36c").removeClass("arrow-top36c");
            resType=$(this).attr("data-resType");
            requestData6=JSON.stringify({"code":"getLocalService","version":"1.0","data":{"productType":"SERVICE","address":currentCity,"serviceId":serviceId,"resType":resType,"currentPage":currentPage,"showCount":2}});
             getData(url,requestData6,handdle_func1,async,type);
        });
        $(".shadow").on("click",function(){
            $(".choose").css("display","none");
            $(".sort").css("display","none");
            var attrS=$(".near-service").find("i").attr("class");
            var attrX=$(".new-service").find("i").attr("class");
            var attrx=attrX.split(" ");
            var attrs=attrS.split(" ");
            if(attrs[1]==="arrow-top36c"){
                $(".near-service").find("i").removeClass("arrow-bottom").removeClass("arrow-top36c").addClass("arrow-bottom36c");
            }else if(attrx[1]==="arrow-top36c"){
                $(".new-service").find("i").removeClass("arrow-bottom").removeClass("arrow-top36c").addClass("arrow-bottom36c");
            }
            $(this).css("display","none");
        });
    var curr_city;
    var zwlarea;
    
        var currentPage=1;//默认设置当前页为第一页
        var totalPage=1;//设置默认总页数
        var show_count=3;//设置默认每次加载的条数
        var currentPage=1;
        var requestData2="";
        var requestData3="";
        var requestData4="";
        var serviceName="";
        var areaName="";
   
        var requestData5="";
        var requestData6="";
        var resType="1";
        
        var url=SYS_GLOBAL_CONFIG.reqUrl,
        async=true,
        type="post";
         
        var requestData=JSON.stringify({
                "code":"getLocalService",
                "version":"1.0",
                "data":{
                    "productType":"SERVICE",
                    "address":currentCity,
                    "serviceId":serviceId,
                    "resType":resType,
                    "currentPage":currentPage,
                    "showCount":show_count
                }
        });          
        getData(url,requestData,handdle_func1,async,type);  
        console.log(serviceId+"服务id")
        var requestData1=JSON.stringify(
            {
                "code":"getServiceTypeList",
                "version":"1.0",
                "data":{
                    "parentServiceId":serviceId
                }
            });
        getData(url,requestData1,handdle_func2,async,type);
        function getData(url,datas,call_func,async,type){
            console.log("qingqiucanshu:"+datas);
            $.ajax({
                url:url,

                type:type,

                datatype:"json",
                
                async:async,

                contentType:"application/json",

                data:datas,

                success:function(data){
                    call_func(data);

                },
                error:function(){
                    console.log("失败-----------");
                }       
            });        
        }
    //服务
        function handdle_func1(data){
            console.log(data);        
            $(".serviceDetail").html(""); 
            for(var i=0;i<data.data.datas.length;i++){       
                var pic="";
                var fulladdress=data.data.datas[i].address;
                var addr=fulladdress.split(" ");
                if(data.data.datas[i].serviceImgs.length>0){
                    for(var j=0;j<data.data.datas[i].serviceImgs.length;j++){
                        if(j==3){
                            break;
                        }
                        pic+='<img class="l"  src="'+data.data.datas[i].serviceImgs[j].FULLPATH+'" alt="">'
                     }
                }else{
                    pic="";
                }
                if(data.data.datas[i].userAuthStatus==1){
                    img1='<img class="img-v" src="./images/vip_icon.png" alt="">';
                      
                }else{
                    img1=""; 
                }
                var li='<div class="s-detail">'+
                        '<aside class="l-aside l">'+
                            '<img src="'+data.data.datas[i].userImgPath+'" alt="">'+
                        '</aside>'+
                        '<aside class="r-aside l">'+
                            '<div class="raside-name clear">'+
                                '<div class="pro l">'+
                                    '<p class="pro-name">'+data.data.datas[i].productName+'</p>'+
                                     '<i>'+data.data.datas[i].saleprice+'/'+data.data.datas[i].unit+'</i>'+
                                    '<p class="user-name"><span class="small-light">'+data.data.datas[i].userName+'</span>'+img1+'<span class="zwl-as">'+addr[2]+'/'+addr[3]+'</span></p>'+
                                '</div>'+

                                '<div class="clears"></div>'+
                            '</div>'+
                            '<p class="raside-explain">'+data.data.datas[i].remark+'</p>'+
                            '<div class="raside-img" data-productId="'+data.data.datas[i].productId+'">'+pic+'<div class="clears"></div>'+
                            '</div>'+
                            '<div class="raside-detail ">'+
                                   '<span class="detail-info">'+data.data.datas[i].parentServiceName+'</span>'+
                                   '<img class="mag-l20" src="./images/shoucang.png" alt=""><span class="detail-info">'+data.data.datas[i].serviceCollectCount+'</span>'+
                                   '<img class="mag-l20"src="./images/liulan.png" alt=""><span class="detail-info">'+data.data.datas[i].viewCount+'</span>'+
                            '</div>'+
                        '</aside>'+
                        '<div class="clears"></div>'+
                    '</div>';
                   $(".serviceDetail").append(li);      
            }
            $(".currentpage").html(data.data.currentPage);
            $(".raside-img").on("click",function(){
                var productId=$(this).attr("data-productId");
                location.href="./serviceDetail.html?productId="+productId+"&userId="+userId;
            });
        }  
    //服务类型列表
        function handdle_func2(data){
            var lison="";
            console.log(data); 
           var lison1='<li class="li1" data-sName="'+serviceId+'"><a class="classify-sort bgc36c7b7">全部</a></li>';
            for(var i=0;i<data.data.serviceTypes.length;i++){
              lison+=' <li class="li1" data-sName="'+data.data.serviceTypes[i].serviceId+'"><a class="classify-sort bgf0f0f0 ">'+data.data.serviceTypes[i].serviceName+'</a></li>';   
            } 
            $(".classify-ul").html(lison1+lison);
            $(".li1").on("click",function(){
                 $('html').css({"overflow":"auto"});
                $('body').css({"overflow":"auto"});
                $(this).find("a").addClass("bgc36c7b7").removeClass("bgf0f0f0");
                $(this).siblings().find("a").removeClass("bgc36c7b7").addClass("bgf0f0f0");
                
             
               $(".serviceSelect").hide();
               $(".shadow").css("display","none");
              $(".choose-name1").css("color","#666").siblings("i").addClass("arrow-bottom").removeClass("arrow-top36c");   
            $(".sort").hide();
             $(".choose-name2").css("color","#666").siblings("i").addClass("arrow-bottom").removeClass("arrow-top36c");
                
                    $(".serviceDetail").html("");
                    serviceName=$(this).attr("data-sName");
                     requestData2=JSON.stringify(
                         {
                             "code":"getLocalService",
                             "version":"1.0",
                             "data":{
                                 "productType":"SERVICE",
                                 "address":currentCity,
                                 "serviceId":serviceName,
                                 "resType":resType,
                                 "currentPage":currentPage,
                                 "showCount":show_count
                             }
                         });
                    getData(url,requestData2,handdle_func1,async,type);
                current_Page=1;
             });
        } 
    //获取区域列表
        function handdle_func3(data){
             $(".area-ul").html("");
            console.log(data); 
            for(var i=0;i<data.data.dictAreas.length;i++){
               var areaLi='<li class="area-li " data-areaName='+data.data.dictAreas[i].fullName+'>'+data.data.dictAreas[i].areaName+'</li>';
                $(".area-ul").append(areaLi);
            }
            $(".area-ul .area-li").off();
            $(".area-ul .area-li").on("click",function(){
                zwlarea=$(this).html();
                $(".detail-area-ul").html("");
                $(this).addClass("bgf036c").siblings(".area-li").removeClass("bgf036c");
                areaName=$(this).attr("data-areaName");
                if(areaName!=""){
                    curr_city=areaName;
                }else{
                    curr_city=currentCity;
                }
                requestData4=JSON.stringify(
                    {
                        "code":"getDictAreaList",
                        "version":"1.0",
                        "data":{
                            "level":"3",
                            "fullName":curr_city
                        }
                    });
                //判断是全城还是街道
                if(areaName==""){
                     $(".detail-area-ul").html('<li class="detail-area-li " data-dictAreas='+curr_city+'>全城</li>');
                     $(".detail-area-li").off();
                        $(".detail-area-li").on("click",function(){
                             $('html').css({"overflow":"auto"});
                            $('body').css({"overflow":"auto"});
                             var zareaAddr=$(this).attr("data-dictAreas");
                             requestData5=JSON.stringify(
                                {
                                    "code":"getLocalService",
                                    "version":"1.0",
                                    "data":{
                                        "productType":"SERVICE",
                                        "address":zareaAddr,
                                        "serviceId":serviceId,
                                        "resType":resType,
                                        "currentPage":currentPage,
                                        "showCount":show_count
                                    }
                                });
                            getData(url,requestData5,handdle_func1,async,type);
                            
                            
                            $(this).addClass("bgf036c").siblings(".detail-area-li").removeClass("bgf036c");
                            $(".serviceSelect").hide();
                            $(".shadow").css("display","none");
                            $(".choose-name1").html($(this).html());
                            $(".choose-name1").siblings("i").addClass("arrow-bottom36c").removeClass("arrow-top36c");   

                        });    
                }else{
                    getData(url,requestData4,handdle_func4,async,type);  
                }
            });     
         } 
    //获取街道列表
    function handdle_func4(data){
           console.log(data); 
            for(var i=0;i<data.data.dictAreas.length;i++){
                console.log(data.data.dictAreas[i].fullName+"---"+i);
               var areaLiStress='<li class="detail-area-li " data-dictAreas="'+data.data.dictAreas[i].areaId+'">'+data.data.dictAreas[i].areaName+'</li>';
                $(".detail-area-ul").append(areaLiStress);
            }  
            $(".detail-area-li").off();
            $(".detail-area-li").on("click",function(){
                 $('html').css({"overflow":"auto"});
                $('body').css({"overflow":"auto"});
                areaId=$(this).attr("data-dictAreas"); 
                requestData5=JSON.stringify(
                    {
                        "code":"getLocalService",
                        "version":"1.0",
                        "data":{
                            "productType":"SERVICE",
                            "areaId":areaId,
                            "serviceId":serviceId,
                            "resType":resType,
                            "currentPage":currentPage,
                            "showCount":show_count
                        }
                    });
                getData(url,requestData5,handdle_func1,async,type);
                $(this).addClass("bgf036c").siblings(".detail-area-li").removeClass("bgf036c");
                $(".serviceSelect").hide();
                $(".shadow").css("display","none");
                $(".choose-name1").html($(this).html());
                $(".choose-name1").siblings("i").addClass("arrow-bottom36c").removeClass("arrow-top36c");   
            });    
         } 
    /*首次加载*/
    var current_Page=1;
    /**
     *滚动加载更多*
     **/
    $(window).scroll(function(){
        console.log("滚动");
        
        totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop())-400;
        console.log(totalheight);
        $(".data-loading").removeClass("hide").addClass("show");
        if((parseFloat($(document).height())-400)==totalheight){ 
            console.log(totalPage+"---"+current_Page);
            if(totalPage>current_Page){
                current_Page+=1;
                var data_p=JSON.stringify({
                    "code":"getLocalService",
                    "version":"1.0",
                        "data":{
                         "areaId":areaId,
                        "productType":"SERVICE",
                        "address":currentCity,
                        "serviceId":"",
                        "resType":resType,
                        "currentPage":current_Page,
                        "showCount":show_count
                    }
                });
                getData(url,data_p,handdle_func1,async,type);    
            }else{
                $(".data-loading").html("已加载全部");
            }
        } 
     });
});